package com.zrrd.blog.system.controller;


import com.zrrd.blog.api.entities.SysMenu;
import com.zrrd.blog.api.entities.SysRole;
import com.zrrd.blog.system.req.SysRoleREQ;
import com.zrrd.blog.system.service.ISysRoleService;
import com.zrrd.blog.util.base.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 角色信息表 前端控制器
 * </p>
 *
 * @author chen
 * @since 2023-08-11
 */
@Api(value = "角色管理接口",description = "角色管理接口，提供增删改查")
@RestController
@RequestMapping("/role")
public class SysRoleController {
    @Autowired
    private ISysRoleService sysRoleService;

    @ApiOperation("根据角色查询角色列表")
    @PostMapping("/search")
    public Result search(@RequestBody SysRoleREQ req){
        return sysRoleService.queryPage(req);
    }

    @ApiOperation("新增角色接口")
    @PostMapping
    public Result save(@RequestBody SysRole sysRole){
        sysRoleService.save(sysRole);
        return Result.ok();
    }

    @ApiOperation("根据id查询角色")
    @GetMapping("/{id}")
    public Result view(@PathVariable("id") String id){
        return Result.ok(sysRoleService.getById(id));
    }

    @ApiOperation("根据id更新角色信息接口")
    @PutMapping
    public Result update(@RequestBody SysRole sysRole){
        sysRole.setUpdateDate(new Date());
        sysRoleService.updateById(sysRole);
        return Result.ok();
    }

    @ApiOperation("通过id删除角色信息")
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable("id") String id){
        return sysRoleService.deleteById(id);
    }

    @ApiOperation("根据角色id查找menuid")
    @ApiImplicitParam(name="id",value = "角色id",required = true)
    @GetMapping("/{id}/menu/ids")
    public Result findMenuIdsById(@PathVariable("id") String id){
        return sysRoleService.findMenuIdsById(id);
    }

    @ApiOperation("保存用户角色")
    @PostMapping("/{id}/menu/save")
    public Result saveRoleMenu(@PathVariable("id") String id, @RequestBody List<String> menuIds){
        return sysRoleService.saveRoleMenu(id,menuIds);
    }
}
